<?php  
/*
$Id$
*/


if (!defined('BASEPATH')) exit('No direct script access allowed');

class Sary extends Controller {

	function Sary()
	{
		parent::Controller();
		$this->template['module'] = "album";
		$this->load->model("album_model", "album");
		if(!isset($this->album->album_settings))
		{
			echo __("This module is not yet set up.", "album")  . "<br />";
			echo anchor('admin/album/settings', __("Go here to set it up", "album"));
			exit();
		}
	}
	
	
	function index()
	{
		//show latest
		redirect('album');
	}

	function _header($sid)
	{
		echo 
		"
		<script type=\"text/javascript\">
		jQuery.preloadImages = function() 
		{
		  for(var i = 0; i<arguments.length; i++)
		  {
		    jQuery(\"<img>\").attr(\"src\", arguments[i]);
		  }
		}
		</script>";

	}
	
	
	function ajax($action, $sid = null)
	{
		switch ($action)
		{
			case 'asehoy':
				if (is_null($sid))
				{
					echo __("You didn't choose any foto", "album");
					return;
				}
				if($row = $this->album->get_image(array('where' => array('sid' => $sid))))
				{
					
					$this->template['title'] = $row['title'];
					$this->template['row'] = $row;
					$total = $this->album->get_total_comments(array('where' => array('album_image_comments.sid' => $row['sid'])));
					$start = (($total > 10)? ($total -10) : 0);
					$this->template['start'] = $start;
					$this->template['hevitra'] = $this->album->get_comments(array('start' => $start, 'limit' => 10, 'order_by' => 'album_image_comments.date', 'where' => array('album_image_comments.sid' => $row['sid'])));

					if ($this->session->userdata('count_album_'.$row['id']) != $row['id'])
					{
						$this->session->set_userdata('count_album_'.$row['id'], $row['id']);
						$this->album->update_album($row['aid'], array('view' => 'view+1'));
					}
					
					
					$this->load->view('photo/ajax/asehoy', $this->template);
				}
				else
				{
					$this->session->set_flashdata('notification', __("Album not found", "album"));
					redirect('album');
				}
			
			break;
		}
	}
	
	function asehoy( $sid = null)
	{
		if (is_null($sid))
		{
			$this->session->set_flashdata('notification', __("You didn't choose any foto", "album"));
			redirect('album/lisitra');
			return;
		}
		
		if($row = $this->album->get_image(array('where' => array('sid' => $sid))))
		{
		
			if($row['g_id'] != '0')
			{
				$this->user->require_login();
			}
		
			$this->plugin->add_action('header', array(&$this, '_header'), $sid);
			if ($row['username'] != $this->user->username && !in_array($row['g_id'], $this->user->groups))
			{
				$this->template['title'] = __("Access error", "album");
				$this->template['message'] = __("You are not allowed by the owner of the album to view it", "album") . "<br />" ;
				$this->layout->load($this->template, 'error');
				return;
			}
			
			$this->template['title'] = $row['album_title'] . " - " .  $row['title'];
			$this->template['row'] = $row;
			$total = $this->album->get_total_comments(array('where' => array('album_image_comments.sid' => $row['sid'])));
			$start = (($total > 10)? ($total -10) : 0);
			$this->template['start'] = $start;
			$this->template['hevitra'] = $this->album->get_comments(array('start' => $start, 'limit' => 10, 'order_by' => 'album_image_comments.date', 'where' => array('album_image_comments.sid' => $row['sid'])));

			if ($this->session->userdata('count_album_'.$row['aid']) != $row['aid'])
			{
				$this->session->set_userdata('count_album_'.$row['aid'], $row['aid']);
				$this->album->update_album($row['aid'], array('view' => 'view+1'));
			}
			if ($this->session->userdata('count_image_'.$row['sid']) != $row['sid'])
			{
				$this->session->set_userdata('count_image_'.$row['sid'], $row['sid']);
				$this->album->update_image($row['sid'], array('view' => 'view+1'));
			}
			
			
			$this->layout->load($this->template, 'photo/asehoy');
		}
		else
		{

			$this->template['title'] = __("Photo error", "album");
			$this->template['message'] = __("Photo not found", "album");
			$this->layout->load($this->template, 'error');
		}
		
	}
	
	function rehetra($start = 0)
	{
		$per_page = 20;
		$params['start'] = $start;
		$params['per_page'] = $per_page;
		$params['order_by'] = "album_images.id DESC";
		$params['where'] = "g_id IN ('" . implode("','" , $this->user->groups) . "') ";
		if($this->user->username) $params['where'] .= " OR " . $this->db->dbprefix('album_images') . ".username = '" . $this->user->username . "'" ;

		$this->template['rows'] = $this->album->get_images($params);
		
		
		$this->template['title'] = __("All images", "album");
		$config['uri_segment'] = 4;
		$config['first_link'] = __('First', 'album');
		$config['last_link'] = __('Last', 'album');
		$config['base_url'] = base_url() . 'album/sary/rehetra';
		$config['total_rows'] = $this->album->get_total_images($params);
		$config['per_page'] = $per_page; 
		$config['num_links'] = 20;
		$this->load->library('pagination');
		
		$this->pagination->initialize($config); 

		$this->template['pager'] = $this->pagination->create_links();		
		$this->template['start'] = $start;		
		$this->template['total'] = $config['total_rows'];		
		$this->template['per_page'] = $config['per_page'];		
	
		
		$this->layout->load($this->template, 'photo/index');
	
	}
	
	
	function frompopemail()
	{
		$this->config->load('config');
		$privatekey = $this->config->item('privatekey');
		
	}
	
	
	function ampidiro($aid= null, $sid = null)
	{
		$this->user->require_login();
		if(is_null($aid)) $aid = $this->input->post('aid');
		
		if($aid === false)
		{
			if($albums = $this->album->get_albums(array('where' => array('a.username' => $this->user->username))))
			{
				$this->template['albums'] = $albums;
			}
			else
			{
				$this->session->set_flashdata('notification', __("Please create first an album", "album"));
				redirect('album/ampidiro');
			}
		
		}
		else
		{
			$album = $this->album->get_album(array('where' => array('a.aid' => $aid)));
			if ($album['username'] != $album['username'] && $this->user->level['album'] < LEVEL_ADD)
			{
				$this->session->set_flashdata('notification', __("You cannot add a photo in this album", "album"));
				redirect($this->session->userdata("last_uri"));
			}		
			
			if($albums = $this->album->get_albums(array('where' => array('a.username' => $album['username']))))
			{
				$this->template['albums'] = $albums;
			}
			else
			{
				$this->session->set_flashdata('notification', __("Please create first an album", "album"));
				redirect($this->session->userdata("last_uri"));
			}
		}
		
		
		$this->template['aid'] = $aid;
		
		if(!is_null($sid))
		{
			$this->template['title'] = __("Modify a photo", $this->template['module']);
		}
		else
		{
			$this->template['title'] = __("Add photo", $this->template['module']);
		}
				
		if ($this->input->post('submit'))
		{
			
			if ($sid = $this->input->post('sid'))
			{
				
				$row = $this->album->get_image(array('where' => array('sid' => $sid)));
				if(($this->user->level['album'] < LEVEL_EDIT) && ($this->user->username != $row['username']))
				{
					$this->template['error'][] = __("You cannot edit this photo", "album") ;
					$this->template['row'] = $_POST;
					$this->layout->load($this->template, 'saryampidiro');
					return;
				}

				if($sid = $this->album->save_image())
				{
					$this->session->set_flashdata('notification', __("Photo updated", "album"));
					redirect("album/asehoy/" . $aid);
				}
				else
				{
					$this->session->set_flashdata('notification', __("There was an error", "album"));
					redirect($this->session->userdata("last_uri"));
				}
				
			}
			else
			{

				if ($sid = $this->album->save_image())
				{
					$this->plugin->do_action('album_save_image', $sid);
					$this->session->set_flashdata('notification', __("Photo saved", "album"));
					redirect("album/asehoy/" . $aid);
				}
				else
				{
					echo "error";
					return;
				}
			}
		}
		
		

		$row = array();
		
		if ($last_post = $this->session->userdata("last_post"))
		{
			$this->template['row'] = $last_post;
			$this->session->set_userdata("last_post", array());
		}
		
		if (!is_null($sid))
		{
			$row = $this->album->get_image(array('where' => array('sid' => $sid)));
			if($row['username'] != $this->user->username)
			{
				//check only if not the submitter
				$this->user->check_level($this->template['module'], LEVEL_EDIT);
			}
			$this->template['row'] = $row;
		}
		
		$this->layout->load($this->template, 'photo/ampidiro');
	
	}
	
	function fafao( $sid , $js = 0)
	{
		$this->user->require_login();
		$this->template['title'] = __("Delete an album", "album");
		if (is_null($sid))
		{
			$this->session->set_flashdata("notification", __("Please choose an image.", "album"));
			
			redirect('last_uri');
			return;
		}
		
		
		if ($js == 0)
		{
			$this->template['row'] = $this->album->get_image(array('where' => array('sid' => $sid)));
			$this->layout->load($this->template, "photo/delete");
			return;
		}
		
		$row = $this->album->get_image(array('where' => array('sid' => $sid)));
		
		if($row['username'] != $this->user->username)
		{
			//check only if not the submitter
			$this->user->check_level($this->template['module'], LEVEL_DEL);
		}
		
		if ($this->album->delete_image($sid))
		{
			$this->plugin->do_action('album_delete_image', $row);
			$this->session->set_flashdata("notification", __("Image deleted"));
			redirect("album/asehoy/" . $row['aid']);
		}
		else
		{
			$this->session->set_flashdata("notification", __("Unknown error.", "album"));
			
			redirect($this->user->last_uri);
		}
	}

	function rss($type = null)
	{
		$params = array();
		$params['limit'] = 10;
		switch ($type)
		{
			case 'random':
				$params['order_by'] = 'RAND()';
			break;
			default:
				$params['order_by'] = 'id DESC';
			break;
		}
		
		if($rows = $this->album->get_images($params))
		{
			$this->load->helper('xml');
			$data['encoding'] = 'utf-8';
			$data['feed_name'] = $this->system->site_name;
			$data['feed_url'] = base_url();
			$data['page_description'] = $this->system->meta_description;
			$data['page_language'] = $this->user->lang;
			$data['creator_email'] = (isset($this->system->admin_email))? $this->system->admin_email : "";
			
			
			foreach ($rows as $key => $row)
			{
				$contents[$key]['title'] =  $row['title'];
				$contents[$key]['url'] = site_url('album/sary/asehoy/' . $row['sid']);
				$contents[$key]['body'] = "<img src='" . $row['s'] . "' align='left'> " . $row['description'];
				$contents[$key]['date'] = (isset($row['date'])) ? $row['date'] : '';
				$contents[$key]['img'] = $row['o'];
				$contents[$key]['media'] = $row;
			}
			$data['contents'] = $contents;
			header("Content-Type: application/xml");
			
			$this->load->view('rss', $data);
		}
	}

	function search($tosearch = null)
	{
		if(is_null($tosearch))
		{
			//search form
		}
		else
		{
			$params = array('where' => 'album_images.title = ' . $this->db->escape($tosearch));
			$search_id = $this->album->save_tadiavo(serialize($params));
			$this->result($search_id);
		}
	}
	
	function result($search_id = 0, $start = 0)
	{
		if ($search_id != 0 && $tmp = $this->album->get_tadiavo($search_id))
		{
			$params = unserialize( $tmp);
			
		}
		
		$params['where'] .= " AND g_id IN ('" . implode("', '" , $this->user->groups) . "') " ;

		$per_page = 20;
		$params['start'] = $start;
		$params['per_page'] = $per_page;
		$params['order_by'] = "album_images.id DESC";

		$this->template['rows'] = $this->album->get_images($params);
		

		
		$this->template['title'] = __("Search result", "album");
		$config['uri_segment'] = 5;
		$config['first_link'] = __('First', 'album');
		$config['last_link'] = __('Last', 'album');
		$config['base_url'] = base_url() . 'album/sary/result/' . $search_id;
		$config['total_rows'] = $this->album->get_total_images($params);
		$config['per_page'] = $per_page; 
		$config['num_links'] = 20;

		$this->load->library('pagination');
		
		$this->pagination->initialize($config); 

		$this->template['pager'] = $this->pagination->create_links();		
		$this->template['start'] = $start;		
		$this->template['total'] = $config['total_rows'];		
		$this->template['per_page'] = $config['per_page'];		
	
		
		$this->layout->load($this->template, 'photo/photo_list');
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	function maromaro($aid= null, $sid = null)
	{
		if(is_null($aid))
		{
			$this->session->set_flashdata('notification', __("Please select an album", "album"));
			redirect($this->session->userdata("last_uri"));
		}
		
		$this->template['title'] = __("Add more photos", $this->template['module']);
		$this->template['aid'] = $aid;
		$this->user->require_login();
		
		$album = $this->album->get_album(array('where' => array('a.aid' => $aid)));
		
		if ($album['username'] != $album['username'] && $this->user->level['album'] < LEVEL_ADD)
		{
			$this->session->set_flashdata('notification', __("You cannot add a photo in this album", "album"));
			redirect($this->session->userdata("last_uri"));
		}		
		
				
		if ($this->input->post('submit'))
		{

	        $config['upload_path'] = './uploads/'; // server directory
	        $config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
	        $config['max_size']    = '1000'; // in kb
	        $config['max_width']  = '1024';
	        $config['max_height']  = '768';
	        
	        $this->load->library('upload', $config);
	        $this->load->library('Multi_upload');
	    
	        $files = $this->multi_upload->go_upload();
	        
	        if ( ! $files )        
	        {
	            $this->template['error'] = $this->upload->display_errors();
	            $this->layout->view($this->template, 'photo/multiple-upload');
	        }    
	        else
	        {
	            $data = array('upload_data' => $files);
				foreach ($data as $image_data)
				{
					
				}
	            $this->load->view('upload_success', $data);
	        }
		
		
			if ($this->album->save_image())
			{
				$this->session->set_flashdata('notification', __("Photo saved", "album"));
				redirect("album/asehoy/" . $aid);
			}
			else
			{
				echo "error";
				return;
			}
		}
		else
		{
			$this->layout->view($this->template, 'photo/multiple-upload');	
		}		
		
	}

	
}